import axios from 'axios';

class HttpRequest {
    constructor(options){
        this.defaults = {
            baseUrl:''
        }
        this.defaults = Object.assign(this.defaults, options);
    }

    setConfig(){

    }

    interceptors(install){
        // 拦截请求
        install.interceptors.request.use( (config) =>{
            let token = localStorage.getItem('token');
            if (token) {
                config.headers.authorization = `token${token}`;
            }
            return config
        },
        (err) =>{
            return Promise.reject(err) ;
        });
        // 拦截响应
        install.interceptors.response.use( (res)=>{
            const {data, status } = res ;
            if (status > 100){
                return data ;
            }
        },
        (err) => {
            return Promise.reject(err);
        }
        )

    }

    request(options){
        options = Object.assign(this.defaults, options);
        const instance =  axios.create(options) ;
        this.interceptors(instance);
        return instance ;
    }

}


const request  = new HttpRequest({baseURL: '/api'});
const http = request.request();

export async function getBanner (){
    return await http.get('/banner');
}

export async function getProperty(){
    return  http.get('/menu/property');
  }
  // 获取所有菜谱分类
  export async function getClassify(){
    return  http.get('/menu/classify');
  }
/**
*拿到用户发布的菜谱，可做筛使用选
*@params {Object} [params] - 不填写，则获取所有的菜谱
*@params {string} [params.userId] - 指定用户的菜单
*/
  export async function getMenus(params){
    return await http.get('/menu/query', {params});
  }